package com.googlecode.lookie.preview;

import java.awt.Image;

import javax.swing.JComponent;
import javax.swing.JScrollPane;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.richclient.application.support.AbstractView;

import com.googlecode.lookie.core.log.DefaultLogRecord;
import com.googlecode.lookie.core.log.Log;
import com.googlecode.lookie.core.log.LogRecord;
import com.googlecode.lookie.core.log.LogRecord.Severity;
import com.googlecode.lookie.listview.ImageSelectionEvent;
import com.googlecode.lookie.platform.image.format.Format;
import com.googlecode.lookie.platform.image.format.FormatResolver;
import com.googlecode.lookie.platform.image.io.Reader;
import com.googlecode.lookie.platform.image.io.ReaderRegistry;
import com.googlecode.lookie.platform.image.resource.FileResource;
import com.googlecode.lookie.preview.ImagePreviewOptions.FitMode;

public class PreviewView
    extends AbstractView
    implements ApplicationListener
{

    private ImagePreview preview;

    public FormatResolver getFormatResolver()
    {
        return (FormatResolver) getApplicationServices().getService( FormatResolver.class );
    }

    public ReaderRegistry getReaderRegistry()
    {
        return (ReaderRegistry) getApplicationServices().getService( ReaderRegistry.class );
    }

    @Override
    protected JComponent createControl()
    {
        log( Severity.INFO, "message" );

        preview = new ImagePreview();
        preview.getOptions().setFitMode( FitMode.FIT_BIG );

        JScrollPane scrollPane = getComponentFactory().createScrollPane( preview );

        scrollPane.setSize( 400, 800 );

        return scrollPane;
    }

    private void log( Severity severity, String message )
    {
        Log log = (Log) getApplicationServices().getService( Log.class );
        LogRecord logRecord = new DefaultLogRecord( "test", severity, message );
        log.log( logRecord );
    }

    public void onApplicationEvent( ApplicationEvent event )
    {
        if ( event instanceof ImageSelectionEvent )
        {
            final ImageSelectionEvent e = (ImageSelectionEvent) event;

            Image i = null;
            if ( e.getFile() != null )
            {
                try
                {
                    Format format = getFormatResolver().getFormatByName( ( e.getFile() ).getAbsolutePath() );
                    Reader reader = getReaderRegistry().getReader( format );

                    i = reader.read( new FileResource( ( e.getFile() ) ) );
                }
                catch ( Exception ex )
                {
                    // TODO Auto-generated catch block
                    ex.printStackTrace();
                }
            }

            final Image image = i;
            DefaultImagePreviewModel model = (DefaultImagePreviewModel) preview.getModel();

            model.setImageSource( new ImageSource()
            {

                public Image getImage()
                {
                    return image;
                }

            } );
        }
    }
}
